<template>
    <div>
        <!-- 行内表单 -->
        <el-form :inline="true" :model="categoryForm" class="demo-form-inline">          
            <el-form-item label="一级分类">
                <el-select v-model="categoryForm.category1Id" placeholder="请选择" @change="category1Change" :disabled="isAddOrUpdate">
                    <el-option :label="c1.name" :value="c1.id" v-for="(c1,index) in list1" :key="c1.id"></el-option>                 
                </el-select>
            </el-form-item>
            <el-form-item label="二级分类">
                <el-select v-model="categoryForm.category2Id" placeholder="请选择"@change="category2Change" :disabled="isAddOrUpdate">
                    <el-option :label="c2.name" :value="c2.id" v-for="(c2,index) in list2" :key="c2.id"></el-option>                    
                </el-select>
            </el-form-item>
            <el-form-item label="三级分类">
                <el-select v-model="categoryForm.category3Id" placeholder="请选择" @change="category3Change" :disabled="isAddOrUpdate">
                    <el-option :label="c3.name" :value="c3.id" v-for="(c3,index) in list3" :key="c3.id"></el-option>         
                </el-select>
            </el-form-item>
           
        </el-form>
    </div>
</template>

<script>
export default {
    name: 'CategorySelect',
    props: ["isAddOrUpdate"],
    data(){
        return {
            list1:[],
            list2:[],
            list3:[],
            categoryForm:{
                category1Id:'',
                category2Id:'',
                category3Id:'',
            }
        }
    },
    mounted(){
        this.getCategoryList1();
    },
    methods: {
        //获取一级分类
       async  getCategoryList1(){
           const result= await this.$API.attr.reqCategory1List();
           if(result.code==200){
             this.list1=result.data;
           }
        },
        //一级分类更改后
        async category1Change(){
            const result= await this.$API.attr.reqCategory2List(this.categoryForm.category1Id);
            if(result.code==200){
             this.list2=result.data;
             this.categoryForm.category2Id='';
             this.categoryForm.category3Id='';
             this.list3=[];
             this.$emit("getCategoryInfo",this.categoryForm);
           }
        },
        // 二级分类更改后
        async category2Change(){
            const result= await this.$API.attr.reqCategory3List(this.categoryForm.category2Id);
            if(result.code==200){
             this.list3=result.data;
             this.categoryForm.category3Id='';
             this.$emit("getCategoryInfo",this.categoryForm);
           }
        },
        // 三级分类更改后
        category3Change(){
            this.$emit("getCategoryInfo",this.categoryForm);
        }
    }
}
</script>

<style scoped></style>